package 中国.珍林.AI.图片.分类.qwen2v;

import lombok.extern.log4j.Log4j2;
import org.springframework.stereotype.Component;
import 中国.易.工具.Json工具;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.nio.charset.StandardCharsets;
import java.time.Duration;
import java.util.ArrayList;
import java.util.Base64;

@Component
@Log4j2
public class Qwen2v客户端
{
    public String 推理(String 系统提示词, String 推理提示语, byte[] image, Double 温度, 大模型回调 cb)
    {
        HttpClient httpClient = HttpClient.newHttpClient();
        请求 qq = new 请求();
        qq.温度 = 温度;
        qq.消息列表 = new ArrayList<>();
        {
            文本消息 xx = new 文本消息();
            xx.角色 = A消息.E角色.系统;
            xx.内容 = 系统提示词;
            qq.消息列表.add(xx);
        }
        {
            图文消息 xx = new 图文消息();
            qq.消息列表.add(xx);
            xx.角色 = A消息.E角色.用户;
            xx.内容 = new ArrayList<>();
            {
                图片消息体 t = new 图片消息体();
                xx.内容.add(t);
                t.内容 = "data:image;base64," + Base64.getEncoder().encodeToString(image);
            }
            {
                文本消息体 t = new 文本消息体();
                xx.内容.add(t);
                t.内容 = 推理提示语;
            }
        }

        HttpRequest.Builder builder = HttpRequest.newBuilder(URI.create("http://192.168.1.240:8894/ti2t"))
                .header("Content-Type", "application/json").timeout(Duration.ofSeconds(60));
        HttpRequest request = builder.POST(HttpRequest.BodyPublishers.ofString(Json工具.对象转字符串(qq))).build();
        log.info("发送了请求");
        long 首字时间 = 0;
        long t = System.currentTimeMillis();
        StringBuilder sb = new StringBuilder();
        try
        {
            HttpResponse<InputStream> response = httpClient.send(request, HttpResponse.BodyHandlers.ofInputStream());
            int 返回码 = response.statusCode();
            InputStream responseBodyStream = response.body();
            try (BufferedReader reader = new BufferedReader(
                    new InputStreamReader(responseBodyStream, StandardCharsets.UTF_8)))
            {
                String line;
                while ((line = reader.readLine()) != null)
                {
                    if (返回码 != 200)
                    {
                        log.error("服务报{}错误,{}", 返回码, line);
                        return null;
                    }
                    else
                    {
                        if (首字时间 == 0)
                        {
                            首字时间 = System.currentTimeMillis() - t;
                        }
                        cb.响应(line);
                        sb.append(line);
                    }
                }
                cb.结束();
                log.info("首字时间:{}", 首字时间);
            }
        }
        catch (Exception e)
        {
            log.error("调用图文模型异常", e);
            return null;
        }
        return sb.toString();
    }
}
